package com.kaifamiao.month04.day07;

import java.util.Arrays;

public class Sort {

    // 快排
    public static void quickSort(int[] arr, int left, int right) {
        if (left > right) {
            return;
        }
        int temp = arr[left];
        int l = left;
        int r = right;
        while (l < r) {
            while (arr[r] >= temp && l < r) {
                r --;
            }
            while (arr[l] <= temp && l < r) {
                l++;
            }
            int t = arr[r];
            arr[r] = arr[l];
            arr[l] = t;
        }
        arr[left] = arr[r];
        arr[r] = temp;
        quickSort(arr, left, r - 1);
        quickSort(arr, r + 1, right);
    }

    // 插排
    public static void insertSort(int[] arr) {
        for(int i = 1; i < arr.length; i ++) {
            int temp = arr[i];
            int j = i - 1;
            while (j >= 0 && arr[j] > temp) {
                arr[j + 1] = arr[j];
                j --;
            }
        }
    }

    // 冒泡排序
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i ++) {
            for (int j = 0; j < arr.length - 1 - i; j ++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    // 选择排序
    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i ++) {
            int minIndex = i;
            for (int j = minIndex + 1; j < arr.length; j ++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 0, 2, 3, 5, 4, 3};
        insertSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
